<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input class="w500" v-model="form.name" placeholder="例如：测试"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input class="w500" v-model="form.description" placeholder="例如：ceshi"></el-input>
      </el-form-item>
      <el-form-item label="分类图标">
        <el-upload
          :action="upload_url"
          list-type="picture-card"
          :on-success="handleAvatarSuccess">
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="分类属性">
        <el-checkbox-group v-model="form.cate_attrs" >
          <el-checkbox disabled v-for="i in checked_attr" :label="i.id" :key="i.id">{{i.name}}</el-checkbox>
        </el-checkbox-group>
        <el-checkbox-group v-model="cate_attrs2" >
          <el-checkbox v-for="i in no_checked_attr" :label="i.id" :key="i.id">{{i.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="关键词" >
        <el-input class="w500" v-model="form.keywords" ></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="order">
        <el-input class="w500" v-model="form.order" type="number"></el-input>
      </el-form-item>
      <el-form-item label="是否公开">
        <el-radio-group v-model="form.is_public">
          <el-radio label="1">是</el-radio>
          <el-radio label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { mapState } from 'vuex'
  import notify from '@/tools/notify'
  import { UPLOAD_URL } from '@/config/const'
  import { union, pullAll } from 'lodash'

  export default {
    data: function () {
      return {
        form: {
          name: '',
          action: '',
          profile_pic: '',
          pid: 0,
          order: 1,
          app_id: '',
          other_app_read: [],
          is_public: '0'
        },
        cate_attrs2: [],
        checked_attr: [],
        no_checked_attr: [],
        rules: {
          name: [
            {required: true, message: '请输入名称', trigger: 'blur'}
          ],
          description: [
            {required: true, message: '请输入描述', trigger: 'blur'}
          ]
        }
      }
    },
    computed: {
      ...mapState({
        goodsclassattrall: state => state.goodsattr.goodsClassAttrAll,
        goodsparentclassattr: state => state.goodsattr.goodsParentClassAttr,
        classdata: state => state.goodsclass.saveData,
        cover: state => state.goodsclass.cover
      }),
      upload_url () {
        return UPLOAD_URL
      }
    },
    async created () {
      await this.goodsParentClassAttr()
      await this.getattrall()
      this.form.app_id = this.classdata.app_id
      this.form.pid = this.classdata.id
      this.form.other_app_read = this.classdata.other_app_read
      for (let j = 0; j < this.goodsclassattrall.length; j++) {
        for (let i = 0; i < this.goodsparentclassattr.length; i++) {
          if (Number(this.goodsclassattrall[j].id) === Number(this.goodsparentclassattr[i])) {
            this.checked_attr.push(this.goodsclassattrall[j])
          }
          this.goodsparentclassattr[i] = Number(this.goodsparentclassattr[i])
        }
      }
      this.form.cate_attrs = this.goodsparentclassattr
      this.no_checked_attr = pullAll(this.goodsclassattrall, this.checked_attr)
    },
    methods: {
      // 获取全部商品属性
      async getattrall () {
        await this.$store.dispatch('goodsattr/goodsClassAttrAll')
      },
      // 获取父级商品分类属性
      async goodsParentClassAttr () {
        let payload = {
          pid: this.classdata.id
        }
        await this.$store.dispatch('goodsattr/goodsParentClassAttr', payload)
      },
      onSubmit (formName) {
        const self = this
        self.$refs[formName].validate((valid) => {
          if (valid) {
            if (self.cover.save_path === undefined) {
              notify.warning('请上传分类图标！')
              return false
            }
            self.form.profile_pic = 'http://' + self.cover.save_path + '/' + self.cover.filename
            self.form.cate_attrs = union(self.form.cate_attrs, self.cate_attrs2)
            self.$store.dispatch('goodsclass/goodsClassAdd', self.form).then(res => {
              notify.success(res.msg)
              this.$router.push('/goods/goodsclass')
            })
          }
        })
      },
      handleAvatarSuccess (file, response, fileList) {
        const self = this
        if (file.code !== 200) {
          fileList.pop()
          self.$message({
            message: '封面上传失败',
            type: 'error'
          })
        } else {
          self.$message({
            message: '封面上传成功',
            type: 'success'
          })
        }
        self.$store.commit('goodsclass/GOODS_CLASS_COVER', file.res)
      },
      goBack () {
        this.$router.go(-1)
      }
    }
  }
</script>
